from typing import TypeVar, Generic, Optional
from tortoise import Model

ModelType = TypeVar("ModelType", bound=Model)

class BaseService(Generic[ModelType]):
    """基础服务类"""
    
    def __init__(self, model: ModelType):
        self.model = model
    
    async def get(self, id: int) -> Optional[ModelType]:
        """获取单个实例"""
        return await self.model.get_or_none(id=id)
    
    async def get_multi(self, *, skip: int = 0, limit: int = 100):
        """获取多个实例"""
        return await self.model.all().offset(skip).limit(limit)
    
    async def create(self, **data) -> ModelType:
        """创建实例"""
        return await self.model.create(**data)
    
    async def update(self, id: int, **data) -> Optional[ModelType]:
        """更新实例"""
        obj = await self.get(id)
        if obj:
            await obj.update_from_dict(data).save()
        return obj
    
    async def delete(self, id: int) -> bool:
        """删除实例"""
        obj = await self.get(id)
        if obj:
            await obj.delete()
            return True
        return False 